package com.example.messagesample04_2;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.style.BulletSpan;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ActMain extends Activity {

	String[] strUsers=new String[]{"Marco","Tom","Jason","Selina","Mark","July",
			"Mary","Bill","Jacky","Wang","Matt",
			"Marian","Bily","Jack","Wan","Mati",
			"Joey","Bruce","MainChu","WudwenYee","Sun"};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actmain);
		InitialComponent();
	}
	
	DialogInterface.OnClickListener btnOK_click=new DialogInterface.OnClickListener(){
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			lblMessage.setText("Click Dialog");
	}};
	OnClickListener btnDialog_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			Builder build=new Builder(ActMain.this);
			build.setTitle("這是抬頭");
			build.setMessage("這是訊息");
			build.setPositiveButton("OK",btnOK_click);
			Dialog dlg=build.create();
			dlg.show();
			
			
	}};
	DialogInterface.OnClickListener onSelect_click=new DialogInterface.OnClickListener(){
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			lblMessage.setText("Click "+strUsers[arg1]);
	}};	
	
	OnClickListener btnList_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			Builder build=new Builder(ActMain.this);
			build.setTitle("請選擇收件人：");
			build.setItems(strUsers, onSelect_click);
			Dialog dlg=build.create();
			dlg.show();
			
			
	}};
	
	
	String[] rotteryList;

	DialogInterface.OnClickListener onRottery_Select=new DialogInterface.OnClickListener(){
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			//這邊是Dailog選到的
			lblMessage.setText("會中唷:"+rotteryList[arg1]);
	}};
	
	OnClickListener rotty_click=new OnClickListener(){
		public void onClick(View arg0) {
			rotteryList = new String[10] ;
			
			
			for(int i = 0 ; i < rotteryList.length ;i++){	
				Set<Integer> myRoto = new HashSet<Integer>();
				while (myRoto.size() < 6) { 
					myRoto.add((int)(Math.random()*49)+1); 
				}
				List list = new ArrayList(myRoto);
				Collections.sort(list);
				rotteryList[i] = list.toString();
			}	
			//對話方框的使用框
			Builder build=new Builder(ActMain.this);
			//標題
			build.setTitle("請選擇：");
			//擺放字串陣列,後面,會去觸發點選後,是否要觸發onClcik事件
			build.setItems(rotteryList, onRottery_Select);
			//產生這個dialog
			Dialog dlg=build.create();
			//顯示這個dialog
			dlg.show();
			
			
	}};
	
	OnClickListener btnToast_click=new OnClickListener(){
		public void onClick(View arg0) {
			//第一個參數 Context = 這支ActMain.this
			//第二個參數 放文字訊息
			//第三個參數 顯示的長度
			Toast msg =Toast.makeText(
					ActMain.this, 
					"儲存成功",
					Toast.LENGTH_SHORT);
			msg.show();
		
	}};
	
	OnClickListener btnProcess_click=new OnClickListener(){
		public void onClick(View arg0) {
			ProgressDialog l_dialog=new ProgressDialog(ActMain.this);
			//設定讀取長度
			l_dialog.setMax(100);
			//設定這個風格
			l_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			//設定訊息
			l_dialog.setTitle("正在讀取資料");
			//l_dialog.setCancelable(false);
			//目前狀態列
			l_dialog.incrementProgressBy(68);
			l_dialog.show();
		
	}};
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if(item.getItemId()==1)
			lblMessage.setText("貼上");
		else
			lblMessage.setText("複製");
		return super.onContextItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.add(0,0,1,"複製");
		menu.add(0,1,2,"貼上");
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0,0,1,"複製");
		menu.add(0,1,2,"貼上");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==1)
			lblMessage.setText("貼上");
		else
			lblMessage.setText("複製");
		return super.onOptionsItemSelected(item);
	}

	OnClickListener btnNotification_click=new OnClickListener(){
		public void onClick(View arg0) {
			//上面的訊息通知，參數1 圖片，參數2 訊息，參數3 給時間
			Notification notify =new Notification(
	    			android.R.drawable.stat_sys_speakerphone,
	    			"您有3封簡訊未讀取",
	    			System.currentTimeMillis());
	    	
			//要傳送過去的訊息 
			//參數1 context ,參數2 預設0
			//參數3 要從哪個Activity傳送到哪個Activity
			//參數4 Flag更新
	    	PendingIntent pend  = PendingIntent.getActivity(
	    			ActMain.this,
	    			0,
	    			new Intent(ActMain.this,ActMain.class),
	    			PendingIntent.FLAG_UPDATE_CURRENT);
	    	
	    	//訊息的設定
			notify.setLatestEventInfo(
					ActMain.this,
					"簡訊來自Marco",
					"Hello 這是摘要",
					pend);
			
			NotificationManager manager=
					(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
			manager.notify(0,notify);
	
	}};
	

	
	
	private void InitialComponent() {
		
		btnDialog=(Button)findViewById(R.id.btnDialog);
		btnDialog.setOnClickListener(btnDialog_click);
		
		btnList=(Button)findViewById(R.id.btnList);
		btnList.setOnClickListener(btnList_click);
		
		btnRottyList=(Button)findViewById(R.id.rottyList);
		btnRottyList.setOnClickListener(rotty_click);
		
		lblMessage=(TextView)findViewById(R.id.lblMessage);
		
		btnProcess = (Button)findViewById(R.id.btnProcess);
		btnProcess.setOnClickListener(btnProcess_click);
		
		btnToast  =  (Button)findViewById(R.id.btnToast);
		btnToast.setOnClickListener(btnToast_click);
		
		btnNotification  =  (Button)findViewById(R.id.btnNotification);
		btnNotification.setOnClickListener(btnNotification_click);
		//註冊事件
		lblMessage=(TextView)findViewById(R.id.lblMessage);
		registerForContextMenu(lblMessage);
		
	}
   
	Button btnNotification = null;
	Button btnDialog = null;
	Button btnList = null;
	Button btnRottyList = null;
	Button btnReceive =null;
	Button btnProcess =null;
	Button btnToast   =null;
	TextView lblMessage =null;

}
